package logic

import (
	"context"
	"errors"
	"fmt"
	"math/rand"
	"person/model/mysql"
	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *personrpc.LoginRequest) (*personrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	user := &mysql.User{
		//Model:    gorm.Model{},
		Username:  in.Username,
		Password:  in.Password,
		HeadImage: "https://image.baidu.com/search/detail?adpicid=0&b_app",
		Age:       18,
		NickName:  fmt.Sprintf("Per%v%v", time.Now().Unix(), rand.Intn(100000)),
	}
	err := user.FindUserByUsername(mysql.DB, in.Username)
	if err != nil {
		return nil, errors.New("登录失败")
	}
	return &personrpc.LoginResponse{
		UserId: int64(user.ID),
	}, nil
}
